// pages/activity/apply.js
const app = getApp()
import api from '../../utils/api'

Page({
  data: {
    activity_name: '',
    activity_type_index: 0,
    activity_types: [],
    activity_date: '',
    start_time: '',
    end_time: '',
    participant_count: '',
    organizer: '',
    contact_name: '',
    contact_phone: '',
    activity_description: '',
    venue_requirements: '',
    equipment_needs: '',

    canNext: false,
    submitting: false,

    step: 1
  },

  async onLoad() {
    await this.loadActivityTypes()
    this.prefillContact()
  },

  async loadActivityTypes() {
    try {
      const res = await api.activity.getActivityTypes()
      const list = (res.activity_types || []).map(x => x.activity_type || x)
      this.setData({ activity_types: list.length ? list : ['学术讲座','文化展览','教育培训','社团活动','学术研讨','文艺演出','主题沙龙','公益活动'] })
    } catch (e) {
      this.setData({ activity_types: ['学术讲座','文化展览','教育培训','社团活动','学术研讨','文艺演出','主题沙龙','公益活动'] })
    }
  },

  prefillContact() {
    const user = app?.globalData?.userInfo || {}
    this.setData({
      contact_name: this.data.contact_name || user.nickname || user.name || '',
      contact_phone: this.data.contact_phone || user.phone || ''
    })
    this.updateCanNext()
  },

  onInput(e){
    const field = e.currentTarget.dataset.field
    this.setData({ [field]: e.detail.value })
    this.updateCanNext()
  },

  onTypeChange(e){
    this.setData({ activity_type_index: Number(e.detail.value) })
    this.updateCanNext()
  },
  onDateChange(e){ this.setData({ activity_date: e.detail.value }); this.updateCanNext() },
  onStartTimeChange(e){ this.setData({ start_time: e.detail.value }); this.updateCanNext() },
  onEndTimeChange(e){ this.setData({ end_time: e.detail.value }); this.updateCanNext() },

  isValidPhone(phone){ return /^(1\d{10})$/.test(String(phone||'').trim()) },

  updateCanNext(){
    const v = this.data
    const count = Number(v.participant_count)
    const timeOk = v.start_time && v.end_time && v.end_time > v.start_time
    const required = v.activity_date && timeOk && this.isValidPhone(v.contact_phone) && count>=1 && count<=500
    this.setData({ canNext: !!required })
  },

  goNext(){ if(this.data.canNext) this.setData({ step: 2 }) },
  goPrev(){ this.setData({ step: 1 }) },

  async submit(){
    if(this.data.submitting) return

    const isLoggedIn = await app.checkAuth()
    if (!isLoggedIn) return

    this.setData({ submitting: true })
    try{
      const payload = {
        activity_name: this.data.activity_name || '未命名活动',
        activity_type: this.data.activity_types[this.data.activity_type_index],
        activity_date: this.data.activity_date,
        start_time: this.data.start_time,
        end_time: this.data.end_time,
        participant_count: Number(this.data.participant_count),
        organizer: this.data.organizer,
        contact_name: this.data.contact_name,
        contact_phone: this.data.contact_phone,
        activity_description: this.data.activity_description,
        venue_requirements: this.data.venue_requirements,
        equipment_needs: this.data.equipment_needs
      }

      const res = await api.activity.createApplication(payload)
      wx.showModal({
        title: '提交成功',
        content: (res && res.message) || '活动申请提交成功，请等待审核',
        showCancel: false,
        success: ()=>{ wx.navigateTo({ url: '/pages/profile/activities' }) }
      })
    }catch(err){
      wx.showToast({ title: err.message || '提交失败', icon: 'none' })
    }finally{
      this.setData({ submitting: false })
    }
  }
})